<template>
  <t-dialog
    :header="header"
    :visible.sync="visible"
    :onClose="closeDialog"
    :closeOnOverlayClick="false"
    @confirm="onSubmit"
    :confirmBtn="{
        content: loading?'提交中...':'确认',
        theme: 'primary',
        loading: loading,
      }"
    width="50%"
    top="5%"
  >
  <div style="height: 65vh">
    <t-form ref="form" :colon="true"  :data="formData" labelWidth="100px" :rules="rules">
      <t-form-item label="刊物名称" name="name">
        <t-input clearable v-model="formData.name"  placeholder="请输入刊物名称" />
      </t-form-item>
      <t-form-item label="刊物类别" name="periodicalType">
        <t-select clearable v-model="formData.periodicalType" :options="dict.type.periodical_type"></t-select>
      </t-form-item>
      <t-form-item label="刊物等级" name="periodicalLevel">
        <t-select clearable v-model="formData.periodicalLevel" :options="dict.type.periodical_level"></t-select>
      </t-form-item>
      <t-form-item label="刊物级别" name="paperLevel">
        <t-select clearable v-model="formData.paperLevel" :options="dict.type.paper_level"></t-select>
      </t-form-item>
    </t-form>
  </div>
  </t-dialog>
</template>

<script>
export default {
  name: "add-edit-periodical",
  props: ['header', 'objId'],
  dicts: ['periodical_type', 'periodical_level', 'paper_level'],
  data(){
    return {
      rules: {
        name: [
          { required: true, message: "刊物名称不能为空" }
        ],
        periodicalType: [
          { required: true, message: "刊物类别不能为空" }
        ],
        periodicalLevel: [
          { required: true, message: "刊物等级不能为空" }
        ],
        paperLevel: [
          { required: true, message: "刊物级别不能为空" }
        ],
      },
      menuOptions: [],
      visible: true,
      loading: false,
      formData:{
        name: "",
        periodicalType: '',
        periodicalLevel: '',
        paperLevel: '',
      }
    }
  },
  watch: {
    objId: {
      immediate: true,
      handler(val) {
        if (val) {
          this.getObj();
        }
      }
    },
  },
  methods:{
    getObj(){
      this.$api.cgzh.thesisPeriodical.get(this.objId).then(response => {
        if (response.data.parentId === 0){
          response.data.parentId = null;
        }
        this.formData = response.data;
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    onSubmit() {
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {
          this.loading = true;
          if (this.objId) {
            this.$api.cgzh.thesisPeriodical.edit(this.formData).then(() => {
              this.$message.success("修改成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          } else {
            this.$api.cgzh.thesisPeriodical.add(this.formData).then((response) => {
              this.$message.success("新增成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          }
        }
      });
    },
    closeDialog() {
      this.$emit("update:visible", false);
    },
  }
}
</script>
